package com.centling.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.centling.dao.IOrderDao;
import com.centling.entity.Order;
import com.centling.service.IOrderService;
import com.centling.tools.ModifyDate;

/**
 * 订单实现类
 * 
 * @author cyril
 * @date 2017年4月20日
 * @version V1.0
 */
@Service
public class OrderServiceImpl implements IOrderService {

	@Autowired
	public IOrderDao orderDao;
	
	/**
	 * 维护订单
	 */
	@Override
	public void manitainOrder() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
		Date date = new Date();
		String time = sdf.format(date);
		String time2 = sdf2.format(date);
		//产品型号
		String[] productTypeArr = {"XK1","XK2","XK1","XK2","XK1","XK3","XK4","XK4","XK5","XK5"};
		//数量
		int[] countArr = {100,200,10,50,300,100,100,200,150,50};
		//完成状态
		String[] completeStatusArr = {"10%","30%","20%","100%","50%","70%","90%","55%","30%","80%"};
		//业务员
		String[] clerkArr = {"小张","小王","小李","小张","小刘","小吴","小李","小张","小王","小张"};
		
		for (int i=0;i<10;i++) {
			Order order = new Order();
			//订单号D+日期+4位随机数
			String orderNo = "D"+time+(int)(Math.random()*9000+1000);
			order.setOrderNo(orderNo);//订单号
			order.setProductType(productTypeArr[i]);
			order.setCount(countArr[i]);
			//下单时间
			order.setOrderTime(time2);
			//预计交期
			order.setPlanDeliveryTime(time2);
			//完成状态
			order.setCompleteStatus(completeStatusArr[i]);
			//业务员
			order.setClerk(clerkArr[i]);
			orderDao.insertOrder(order);
		}
		
	}

	@Override
	public Integer getOrderCount() {
		return orderDao.getOrderCount();
	}

	/**
	 * 修改订单表格数据
	 */
	@Override
	public void updateOrderData() {
		//获取订单数据List
		List<Order> orderList = orderDao.getOrderList();
		int index = 10;
		//遍历List
		for (Order order : orderList) {
			//获取订单数量
			Integer count = order.getCount();
			//获取订单下单时间
			String orderTime = order.getOrderTime();
			//获取预计交期
			String planDeliveryTime = order.getPlanDeliveryTime();
			//修改下单日期 当日时间+09:index:00
			String setOrderTime = ModifyDate.getTodayDateString()+" 09:"+index+":00";
			order.setOrderTime(setOrderTime);
			if(index<55){
				index += 5;
			}
			//修改预计交期 09替换成16
			String setPlanDeliveryTime = ModifyDate.getTodayDateString()+" 16:"+index+":00";;
			order.setPlanDeliveryTime(setPlanDeliveryTime);
			orderDao.updateOrder(order);
		}
	}

}
